package leetcode.N901_N1000;

/**
 * @author xiwai [hww282306@alibaba-inc.com]
 * @version 1.0
 * @date 2020/10/16 10:59 上午
 */
public class N977 {

    public static void main(String[] args) {
        int[] a = {-4,-1,0,3,10};
        sortedSquares(a);
    }

    public static int[] sortedSquares(int[] A) {
        int length = A.length;
        int leftIndex = 0;
        int rightIndex = length - 1;
        int[] newA = new int[length];
        for (int i = rightIndex; i >= 0; i--) {

            int leftVal = A[leftIndex];
            int rightVal = A[rightIndex];

            if (Math.abs(rightVal) > Math.abs(leftVal)) {
                newA[i] = rightVal * rightVal;
                rightIndex--;
            } else {
                newA[i] = leftVal * leftVal;
                leftIndex++;
            }
        }
        return newA;
    }


}
